package com.ge.modules.api.controller;

import com.ge.common.annotation.LoginUserApi;
import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.api.service.TbRechargeOrderService;
import com.ge.modules.api.service.TbUserCardService;
import com.ge.modules.entity.TbRechargeOrderEntity;
import com.ge.modules.entity.TbUser;
import com.ge.utils.OrderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 充值订单表
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-07-23 18:20:01
 */
@RestController
@RequestMapping("api/user/rechargeorder")
public class TbRechargeOrderController {
    @Autowired
    private TbRechargeOrderService rechargeOrderService;

    @Autowired
    private TbUserCardService userCardService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<TbRechargeOrderEntity> rechargeOrderList = rechargeOrderService.queryList(query);
        int total = rechargeOrderService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(rechargeOrderList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        TbRechargeOrderEntity rechargeOrder = rechargeOrderService.queryObject(id);
        return R.ok().put("rechargeOrder", rechargeOrder);
    }

    /**
     * 创建充值订单
     *
     * @param tbUser
     * @param rechargeType
     * @param money
     * @return
     */
    @PostMapping("/createRechargeOrder")
    public R createRechargeOrder(@LoginUserApi TbUser tbUser, Integer rechargeType, BigDecimal money) {
        TbRechargeOrderEntity tbRechargeOrderEntity = new TbRechargeOrderEntity();
        //判断充值类型 1 余额 2 月卡 3 年卡
        tbRechargeOrderEntity.setCreateTime(new Date());
        tbRechargeOrderEntity.setUserId(tbUser.getUserId());
        tbRechargeOrderEntity.setRechargeOrderCode(OrderUtil.getOrderNo());
        tbRechargeOrderEntity.setPayStatus(1);
        tbRechargeOrderEntity.setRechargeType(rechargeType);
        tbRechargeOrderEntity.setMoney(money);
        rechargeOrderService.save(tbRechargeOrderEntity);
        return R.ok().put("data", tbRechargeOrderEntity);
    }

}
